﻿package com.neriksworkshop.colourLoversDemos.wordPatches.view 
{
	import com.neriksworkshop.lib.colourLovers.events.GraphicItemEvent;
	import com.neriksworkshop.lib.colourLovers.items.IGraphicItem;
	import com.neriksworkshop.lib.colourLovers.items.Palette;
	import com.neriksworkshop.lib.colourLovers.items.Pattern;
	import com.neriksworkshop.lib.tools.RandomTimer;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author Erik
	 */
	public class Patch extends Sprite
	{
		private static const MAX_ITEMS:int = 20;
		private static const BLURB_DIMENSIONS_RANGE:Array = [45, 90];
		
		
		private var _text:String;
		private var _id:int;
		
		private var _tf:TextField;
		
		private var _started:Boolean;
		
		private var _numPalettes:int;
		private var _numPatterns:int;
		
		private var _numPalettesUsed:int;	
		private var _numPatternsUsed:int;
		
		private var _palettes:/*Palette*/Array;
		private var _patterns:/*Pattern*/Array;
		//private var _paletteSprites:/*Object*/Array;
		//private var _patternSprites:/*Object*/Array;
		
		private var _blurbs:/*Blurb*/Array = new Array();
		
		private var _blurbsContainer:Sprite = new Sprite();
		
		
		public function Patch(_text:String, _id:int) 
		{
			this._text = _text;
			this._id = _id;
			
			_tf = new TextField();
			_tf.autoSize = TextFieldAutoSize.LEFT;
			//_tf.antiAliasType = AntiAliasType.ADVANCED;
			//_tf.embedFonts = true			
			_tf.defaultTextFormat = Params.PATCH_FMT_LOADING;
			_tf.filters = Params.PATCH_FILTERS;
			_tf.text = _text;
			
			addChild(_blurbsContainer);
			addChild(_tf);
		}
		
		public function get text():String { return _text; }
		
		public function get id():int { return _id; }

		public function setPalettes(palettes:Array):void
		{
			_numPalettes = palettes.length;
			_palettes = new Array();
						
			for each (var palette:Palette in palettes) 
			{
				palette.createSprite(getBlurbDimensions());
				_palettes.push(palette);
			}
			
			if (!_started) start();
		}
		
		public function setPatterns(patterns:Array):void
		{
			_numPatterns = patterns.length;
						
			for each (var pattern:Pattern in patterns) 
			{
				pattern.createSprite(getBlurbDimensions());
				pattern.addEventListener(GraphicItemEvent.IMAGE_LOADED, onPatternLoaded);
			}
		}		
		
		private function onPatternLoaded(e:GraphicItemEvent):void 
		{
			if (!_patterns) _patterns = new Array();
			_patterns.push((e.target as Pattern));
			
			if (!_started) start();
		}
		
		private function start():void
		{
			_started = true;
			_tf.setTextFormat(Params.PATCH_FMT);
			
			showBlurb(null);
			//var _t:Timer = new Timer(1000, MAX_ITEMS-1);
			//_t.addEventListener(TimerEvent.TIMER, showBlurb);
			//_t.start();
			
			var _t:RandomTimer = new RandomTimer(300, MAX_ITEMS - 1, 1);
			_t.addEventListener(TimerEvent.TIMER, showBlurb);
			_t.start();

		}
		
		private function showBlurb(e:TimerEvent):void 
		{
			//trace("Patch::showBlurb");

			var nextBlurb:Blurb;
						
			if (!_patterns)
			{
				//trace("no pattern, -->palette blurb")
				nextBlurb = createBlurbFromPalette();
			}
			else if (!_palettes)
			{
				nextBlurb = createBlurbFromPattern();
			}
			else
			{
				//trace(_paletteSprites)
				if (_palettes.length == 0 && _patterns.length == 0) 
				{
					//trace('no more items')
					return;
				}
				else
				{
					if (_palettes.length == 0 || _numPatternsUsed < _numPalettesUsed)
					{
						nextBlurb = createBlurbFromPattern();
					}
					else
					{
						nextBlurb = createBlurbFromPalette();
					}
					
				}
			}
			
			var blurbPos:Point = getNextBlurbPos();
			nextBlurb.x = _tf.width / 2 + blurbPos.x;
			nextBlurb.y = _tf.height / 2 + blurbPos.y;
			nextBlurb.addEventListener(MouseEvent.ROLL_OVER, onBlurbOver);
			nextBlurb.addEventListener(MouseEvent.ROLL_OVER, onBlurbOut);
			
			_blurbs.push(nextBlurb);
			_blurbsContainer.addChild(nextBlurb);
			
			
		}
		

		
		private function onBlurbOver(e:MouseEvent):void 
		{
			_blurbsContainer.addChild(e.target as Blurb);
			trace((e.target as Blurb).title)
			_tf.text = (e.target as Blurb).title;
		}
		
		private function onBlurbOut(e:MouseEvent):void 
		{
			//_tf.text = _text;
		}
		
		private function getNextBlurbPos():Point
		{
			//var len:Number = _blurbs.length * (10 + 3 * Math.random());
			//var len:Number = _blurbs.length * 7;
			var len:Number = Math.sqrt(_blurbs.length*13) * 7;
			//var a:Number = Math.sqrt(_blurbs.length*10) * Math.PI/4;
			var a:Number = Math.random() * Math.PI * 2;
			return Point.polar(len, a);
		}
		
		private function createBlurbFromPalette():Blurb
		{
			//trace("next is : PALETTE");
			var nextIndex:int = Math.floor(_palettes.length * Math.random());
			_numPalettesUsed++;
			
			return new Blurb(_palettes.splice(nextIndex, 1)[0]);
		}
		
		private function createBlurbFromPattern():Blurb
		{
			//trace("next is : PATTERN");
			var nextIndex:int = Math.floor(_patterns.length * Math.random());
			_numPatternsUsed++;
			
			
			return new Blurb(_patterns.splice(nextIndex, 1)[0]);
		}		
		
		private function getBlurbDimensions():Rectangle
		{
			var s:Number = BLURB_DIMENSIONS_RANGE[0] + Math.floor((BLURB_DIMENSIONS_RANGE[1] - BLURB_DIMENSIONS_RANGE[0]) * Math.random());
			return new Rectangle(0, 0, s, s);
		}
		/*
		public function build(data:IGraphicItem):void
		{
			_tf.setTextFormat(Params.PATCH_FMT);
			
			var sp:Sprite = data.createSprite();
			trace(sp.stage, this.stage);
			addChild(sp)
			
			//graphics.beginFill(0xFF0000);
			//graphics.drawRect(0, 0, 10, 10);
		}*/
		
	}
	
}